package com.googlecode.restitory.api.gae;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonDesc {

	private Map<String, JsonPropertyDesc> props = new HashMap<String, JsonPropertyDesc>();

	private List<String> keys = new ArrayList<String>();

	public static JsonDesc create() {
		JsonDesc ret = new JsonDesc();
		ret.config(".*").can(true).indexed(true);
		return ret;
	}

	public JsonPropertyDesc config(String regex) {
		JsonPropertyDesc ret = props.get(regex);
		if (ret == null) {
			ret = new JsonPropertyDesc();
			props.put(regex, ret);
			keys.add(0, regex);
		}
		return ret;
	}

	public JsonPropertyDesc get(String name) {
		for (String regex : keys) {
			if (name.matches(regex)) {
				return props.get(regex);
			}
		}
		return null;
	}

	@Override
	public String toString() {
		return "JsonDesc [props=" + props + "]";
	}

}
